<!-- 人员兼职部门 list页面 -->
<template>
  <div class="app-container">
    <div class="form-container">
      <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
        <el-form-item label="人员名称" prop="userId">
          <el-input
            v-model="userName"
            size="small"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <label slot="label">状&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态</label>
          <el-select v-model="queryParams.status" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          />
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="button-wrap">
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:sysUserPartDept:add']" icon="el-icon-plus" size="middle" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:sysUserPartDept:edit']" icon="el-icon-edit" :disabled="single" size="middle" @click="handleEdit">修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          :disabled="single"
          size="mini"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:sysUserPartDept:edit']"
          icon="el-icon-connection"
          :disabled="single"
          size="middle"
          @click="handleMenuAndScope"
        >菜单和数据权限</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="isStatusShow === '1'" v-hasPermi="['system:sysUserPartDept:edit']" :disabled="single" size="middle" icon="el-icon-star-on" @click="handleStatusChange">
          启用
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="isStatusShow === '0'" v-hasPermi="['system:sysUserPartDept:edit']" :disabled="single" size="middle" icon="el-icon-star-off" @click="handleStatusChange">
          停用
        </el-button>
      </el-col>
      <el-col :md="12" :lg="14" :xl="17" class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>

      </el-col>
    </el-row>
    <!-- 表格 -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :data="userPartDeptList"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column prop="partDeptName" align="center" label="兼职部门" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column prop="dataScope" label="权限范围" align="center" min-width="120" :formatter="userPartDeptDataScope" :show-overflow-tooltip="true" />
      <el-table-column prop="status" label="状态" align="center" min-width="60" :formatter="statusFormat" :show-overflow-tooltip="true" />
      <el-table-column prop="partPostName" label="岗位" align="center" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" align="center" min-width="120" :show-overflow-tooltip="true" />
    </el-table>
    <pagination
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <user-part-dept-edit v-if="partEditVisible" ref="partEditData" />
    <!-- 兼职部门分配菜单数据权限 -->
    <part-menu-data v-if="partMenuDataVisible" ref="partMenuData" />
  </div>
</template>

<script>
import { listUserPartDeptByUserId, updateUserPartDept, delUserPartDept } from '@/api/system/user/user-part-dept'
// 人员兼职部门添加、编辑页面
import UserPartDeptEdit from '@/views/system/user/components/UserPartDept/UserPartDeptEdit'
// 人员兼职部门分配菜单和数据权限
import PartMenuData from '@/components/System/User/UserPartDept/PartMenuDataScope'
import { mapState } from 'vuex'
export default {
  components: { UserPartDeptEdit, PartMenuData },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的行对象
      multipleSelection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 人员信息
      userOptions: [],
      // 数据范围选项
      dataScopeOptions: [],
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.fristPageSize,
        userId: '',
        status: ''
      },
      // 高级搜索
      highLevelSet: false,
      userName: '',
      // 启用禁用控制
      isStatusShow: '0',
      // 表格数据
      userPartDeptList: [],
      // 人员兼职部门添加页面
      partEditVisible: false,
      /** 兼职部门分配菜单和数据权限 */
      partMenuDataVisible: false
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created: function() {
    this.getDicts('sys_normal_disable').then(response => {
      this.statusOptions = response.data.rows
    })
    // ---wt (user_part_dept_data_scope ---> role_data_scope)
    this.getDicts('role_data_scope').then(response => {
      this.dataScopeOptions = response.data.rows
    })
    // 路由带过来的参数
    const userId = this.$route.query.userId
    const userName = this.$route.query.userName
    // 搜索框中的roleId
    this.queryParams.userId = userId
    this.userName = userName
    // 加载表格数据
    this.getList()
  },
  methods: {
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.status = ''
      this.dateRange = []
      this.handleQuery()
    },
    // 表格数据加载
    getList() {
      this.loading = true
      listUserPartDeptByUserId(this.changeDateRange(this.queryParams, this.dateRange)).then(resp => {
        if (resp) {
          this.userPartDeptList = resp.data.rows
          this.total = resp.data.total
          this.loading = false
          // this.addTaleData()
        } else {
          this.msgError(resp.message)
        }
      })
    },
    addTaleData() {
      if (this.userPartDeptList.length < this.queryParams.pageSize) {
        const arr = {}
        this.userPartDeptList.push(arr)
        this.addTaleData()
      }
    },
    // 数据权限字典翻译
    userPartDeptDataScope(row, column) {
      console.log(this.dataScopeOptions)
      return this.selectDictLabel(this.dataScopeOptions, row.dataScope)
    },
    // 表格状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      if (JSON.stringify(selection[0]) === '{}') {
        selection = []
        this.$refs.multipleTable.clearSelection()
      }
      this.multipleSelection = selection
      if (this.multipleSelection.length === 1) {
        this.isStatusShow = this.multipleSelection[0].status
      }
      this.ids = selection.map(item => item.postId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      if (selection.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(selection.pop())
      }
    },
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 表格选中高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 添加功能
    handleAdd() {
      const userId = this.queryParams.userId
      this.partEditVisible = true
      this.$nextTick(() => {
        this.$refs.partEditData.showViewAdd(userId)
      })
    },
    // 编辑页面
    handleEdit() {
      const child = this.multipleSelection
      this.partEditVisible = true
      this.$nextTick(() => {
        this.$refs.partEditData.showViewEdit(child[0])
      })
    },
    /* 表格的删除*/
    handleDelete() {
      this.loading = true
      const child = this.multipleSelection
      let partId = ''
      for (let i = 0; i < child.length; i++) {
        partId += child[i].partId + ','
      }
      // 去掉最后一个逗号
      if (partId.length > 0) {
        partId = partId.substr(0, partId.length - 1)
      }
      delUserPartDept(partId).then(resp => {
        if (resp) {
          this.msgSuccess('删除成功!')
          // 重新加载表格数据
          this.getList()
        } else {
          this.msgError(resp.message)
        }
      })
    },

    /** 给兼职部门分配菜单和数据权限 */

    handleMenuAndScope(row) {
      const child = this.multipleSelection
      const partId = child[0].partId
      this.partMenuDataVisible = true
      this.$nextTick(() => {
        this.$refs.partMenuData.showView(partId)
      })
    },
    // 启用禁用操作
    handleStatusChange() {
      const child = this.multipleSelection
      const row = child[0]
      const text = row.status === '0' ? '停用' : '启用'
      this.$confirm('确认要' + text + '' + row.partDeptName + '?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          if (row.status === '0') {
            row.status = '1'
          } else {
            row.status = '0'
          }
          const formdata = {
            'partId': row.partId,
            'status': row.status
          }
          updateUserPartDept(formdata).then(res => {
            if (res) {
              this.getList()
              this.msgSuccess('操作成功！')
            } else {
              this.msgError(res.message)
            }
          })
        })
        .catch(() => {
        })
    },
    // 返回
    handleReturn() {
      this.$router.push({
        path: '/system/user',
        name: '人员'
      })
    }
  }
}
</script>

